package com.spring.sunflower.main.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.spring.sunflower.R;
import com.spring.sunflower.bean.VideoPlayBean;
import com.spring.sunflower.util.cache.PreloadManager;
import com.spring.sunflower.video.LoadingView;
import com.spring.sunflower.video.TikTokView;

import java.util.List;


public class TikTokAdapter extends RecyclerView.Adapter<TikTokAdapter.VideoHolder> {

    private List<VideoPlayBean.DatasBean> videos;
    private Context context;

    public TikTokAdapter(Context context,List<VideoPlayBean.DatasBean> videos) {
        this.videos = videos;
        this.context = context;
    }

    private static final String TAG = "TikTokAdapter";

    public void addData(List<VideoPlayBean.DatasBean> data) {
        videos.addAll(data);
        notifyDataSetChanged();
    }

    public void refreshData(List<VideoPlayBean.DatasBean> data) {
        videos.clear();
        videos.addAll(data);
        notifyDataSetChanged();
    }
    @Override
    public VideoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder: ");
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_item_video_play, parent, false);
        return new VideoHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final VideoHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: " + position);
        VideoPlayBean.DatasBean item = videos.get(position);
        Glide.with(context)
                .load(item.getVideoFirstPhotoUrl())
                .placeholder(R.drawable.ic_bg_video_play)
                .into(holder.thumb);
        holder.mPosition = position;
        PreloadManager.getInstance(holder.itemView.getContext()).addPreloadTask(item.getVideoPath(), position);
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull VideoHolder holder) {
        super.onViewDetachedFromWindow(holder);
        VideoPlayBean.DatasBean datasBean = videos.get(holder.mPosition);
        PreloadManager.getInstance(holder.itemView.getContext()).removePreloadTask(datasBean.getVideoPath());
        holder.loadingView.setTimePeriod(100);
    }

    @Override
    public int getItemCount() {
        return videos.size();
    }

    public static class VideoHolder extends RecyclerView.ViewHolder {

        public ImageView thumb;
        public int mPosition;
        public TikTokView mTikTokView;
        public FrameLayout mPlayerContainer;
        private TextView tvTitle;
        public LoadingView loadingView;
        private LinearLayout lldone,llContainer;

        VideoHolder(View itemView) {
            super(itemView);
            thumb = itemView.findViewById(R.id.iv_thumb);
            mTikTokView = itemView.findViewById(R.id.tiktok_View);
            mPlayerContainer = itemView.findViewById(R.id.flContainer);
            tvTitle = itemView.findViewById(R.id.tv_title);
            loadingView = itemView.findViewById(R.id.loadView);
            lldone = itemView.findViewById(R.id.llDone);
            llContainer = itemView.findViewById(R.id.llContainer);
            lldone.setVisibility(View.GONE);
            llContainer.setVisibility(View.GONE);
            itemView.setTag(this);
        }
    }
}